<template>
    <div class="button-group" v-if="showSave && isShow">
        <Button type="primary" @click="saveBtnAction" :loading="loading">{{ btnName }}</Button>
    </div>
</template>

<script>
import {
  // create,
  update } from '@/api/setting'

export default {
  name: 'CommonSaveBtn',
  props: {
    showSave: {
      type: Boolean,
      default: () => {
        return true
      }
    },
    formData: {
      type: Array,
      default: () => {
        return []
      }
    },
    dbName: {
      type: String,
      default: () => {
        return ''
      }
    },
    paramFilter: {
      type: String,
      default: () => {
        return ''
      }
    },
    beforeHandle: {
      type: String,
      default: () => {
        return ''
      }
    },
    baseInfoId: {
      type: String,
      default: () => {
        return ''
      }
    }
  },
  data () {
    return {
      loading: false,
      isShow: true,
      btnName: '保存'
    }
  },
  methods: {
    saveBtnAction () {
      this.$emit('saveAction')
      this.saveForm()
    },
    beforeHandleAction () {
      // console.log('beforeHandle: ', this.beforeHandle)
      // console.log('传入表单: ', this.formData)
      let newData = []
      if (this.beforeHandle === 'node') {
        this.formData.forEach(item => {
          // console.log('外层单项', item)
          if (item.node && item.node.length > 0) {
            const obj = item
            item.node.forEach(item2 => {
              newData.push({
                ...obj,
                gov_flow_id: item2
              })
            })
          }
        })
        return newData
      } else {
        return this.formData
      }
    },
    saveForm (data) {
      this.loading = true
      let saveData = []
      if (data) {
        saveData = data
      }
      saveData = this.beforeHandleAction()
      // console.log('---最终结果:', saveData)
      if (this.paramFilter !== '') {
        let nowData = []
        saveData.forEach(item => {
          // console.log(item, this.paramFilter)
          if (!this.$common.xeUtils.isEmpty(item[this.paramFilter])) {
            item[this.paramFilter].forEach(item1 => {
              item1.gov_flow_id = item.id
              item1.base_info_id = this.baseInfoId
              nowData.push(item1)
            })
          }
        })
        saveData = nowData
        // console.log(nowData)
      }
      for (let i = 0; i < saveData.length; i++) {
        saveData[i].base_info_id = this.baseInfoId
        if (saveData[i].id) {
          // console.log(saveData[i].id)
        } else {
          saveData[i].id = ''
        }
      }
      let arr = this.$common.xeUtils.clone(saveData)
      // 设定要件
      if (this.dbName === 'GovYaojian') {
        for (let i = 0; i < arr.length; i++) {
          if (!arr[i].yj_title) {
            this.$Message.warning('请检查要件名称')
            this.loading = false
            return false
          }
          if (arr[i].node) {
            if (arr[i].node.length > 1) {
              arr[i].gov_flow_id = arr[i].node.toString()
            } else {
              arr[i].gov_flow_id = arr[i].node[0]
            }
          } else {
            this.$Message.warning('请检查流程节点')
            this.loading = false
            return false
          }
          if (!arr[i].yj_type) {
            this.$Message.warning('请检查要件类型')
            this.loading = false
            return false
          }
          // if (!arr[i].yi_img_demo) {
          //   this.$Message.warning('请检查要件样本')
          //   this.loading = false
          //   return false
          // }
          if (arr[i].yj_type === '1' && !arr[i].is_fu_yong) {
            this.$Message.warning('请检查可复用要件')
            this.loading = false
            return false
          }
          if (arr[i].yj_type === '1' && !arr[i].is_xian_chang_cai_ji) {
            this.$Message.warning('请检查现场采集')
            this.loading = false
            return false
          }
          if (arr[i].upload_type) {
            arr[i].file_type = arr[i].upload_type.toString()
          }
          // else {
          //   this.$Message.warning('请检查上传方式')
          //   this.loading = false
          //   return false
          // }
        }
      }
      // 设定条件
      if (this.dbName === 'GovWhere') {
        for (let i = 0; i < arr.length; i++) {
          if (!arr[i].tj_content) {
            this.$Message.warning('请检查条件内容')
            this.loading = false
            return false
          }
          if (arr[i].node) {
            arr[i].gov_flow_id = arr[i].node.join(',')
          } else {
            this.$Message.warning('请检查流程节点')
            this.loading = false
            return false
          }
          if (!arr[i].tj_xian_zhi) {
            this.$Message.warning('请检查条件限制')
            this.loading = false
            return false
          }
          if (arr[i].tj_xian_zhi === '2' && !arr[i].lh_yi_ju) {
            this.$Message.warning('请检查联合规则')
            this.loading = false
            return false
          }
        }
      }
      // 设定规则
      if (this.dbName === 'GovRule') {
        for (let i = 0; i < arr.length; i++) {
          if (!arr[i].gz_content) {
            this.$Message.warning('请检查规则内容')
            this.loading = false
            return false
          }
          if (arr[i].node) {
            arr[i].gov_flow_id = arr[i].node.join(',')
          } else {
            this.$Message.warning('请检查流程节点')
            this.loading = false
            return false
          }
          if (!arr[i].gz_type) {
            this.$Message.warning('请检查规则类型')
            this.loading = false
            return false
          }
          if (arr[i].gz_type === '2' && !arr[i].lh_yi_ju) {
            this.$Message.warning('请检查联合规则')
            this.loading = false
            return false
          }
        }
      }
      // 设定权限
      if (this.dbName === 'GovPower') {
        for (let i = 0; i < arr.length; i++) {
          if (!arr[i].xq_task) {
            this.$Message.warning('请检查节点任务')
            this.loading = false
            return false
          }
          // if (!arr[i].xq_content) {
          //   this.$Message.warning('请检查节点权限')
          //   this.loading = false
          //   return false
          // }
          // if (!arr[i].xq_bu_men) {
          //   this.$Message.warning('请检查行权部门')
          //   this.loading = false
          //   return false
          // }
          // if (!arr[i].xq_ke_shi) {
          //   this.$Message.warning('请检查行权处室')
          //   this.loading = false
          //   return false
          // }
          // if (!arr[i].xq_people) {
          //   this.$Message.warning('请检查行权人员')
          //   this.loading = false
          //   return false
          // }
        }
      }
      // 设定时限
      if (this.dbName === 'GovTime') {
        for (let i = 0; i < arr.length; i++) {
          if (!arr[i].sj_num) {
            this.$Message.warning('请检查节点时限')
            this.loading = false
            return false
          }
        }
      }
      // 设定数据对比
      if (this.dbName === 'GovDataCompare') {
        for (let i = 0; i < arr.length; i++) {
          if (!arr[i].db_database) {
            this.$Message.warning('请检查对比库')
            this.loading = false
            return false
          }
          // if (!arr[i].db_content) {
          //   this.$Message.warning('请检查对比内容')
          //   this.loading = false
          //   return false
          // }
        }
      }
      // 设定轮巡周期
      if (this.dbName === 'GovLoop') {
        for (let i = 0; i < arr.length; i++) {
          if (!arr[i].lx_type) {
            this.$Message.warning('请检查轮巡类型')
            this.loading = false
            return false
          }
          // if (!arr[i].lx_content) {
          //   this.$Message.warning('请检查轮巡内容')
          //   this.loading = false
          //   return false
          // }
          if (!arr[i].lx_zhou_qi) {
            this.$Message.warning('请检查轮巡周期')
            this.loading = false
            return false
          }
        }
      }
      // 设定公示评价
      if (this.dbName === 'GovEvaluate') {
        for (let i = 0; i < arr.length; i++) {
          if (!arr[i].pj_zhu_ti) {
            this.$Message.warning('请检查评价主体')
            this.loading = false
            return false
          }
          // if (!arr[i].pj_fan_wei) {
          //   this.$Message.warning('请检查评价内容')
          //   this.loading = false
          //   return false
          // }
          // if (!arr[i].pj_fang_shi) {
          //   this.$Message.warning('请检查评价方式')
          //   this.loading = false
          //   return false
          // }
          // if (!arr[i].pj_fan_kui) {
          //   this.$Message.warning('请检查反馈方式')
          //   this.loading = false
          //   return false
          // }
          // if (!arr[i].pj_content) {
          //   this.$Message.warning('请检查调查问卷内容')
          //   this.loading = false
          //   return false
          // }
          // if (!arr[i].pj_gui_ze) {
          //   this.$Message.warning('请检查评价规则')
          //   this.loading = false
          //   return false
          // }
        }
      }
      // 设定监督节点
      if (this.dbName === 'GovCheckNode') {
        for (let i = 0; i < arr.length; i++) {
          if (!arr[i].jd_content) {
            this.$Message.warning('请检查风险点')
            this.loading = false
            return false
          }
          if (arr[i].jdfw) {
            arr[i].jd_fan_wei = arr[i].jdfw.toString()
          } else {
            this.$Message.warning('请检查监督范围')
            this.loading = false
            return false
          }
        }
      }
      // 设定预警报警
      if (this.dbName === 'GovWarning') {
        for (let i = 0; i < arr.length; i++) {
          if (!arr[i].yj_content) {
            this.$Message.warning('请检查预警报警内容')
            this.loading = false
            return false
          }
          if (!arr[i].yj_type) {
            this.$Message.warning('请检查类型')
            this.loading = false
            return false
          }
          if (!arr[i].yj_deng_ji) {
            this.$Message.warning('请检查预警报警等级')
            this.loading = false
            return false
          }
          // if (!arr[i].yj_fang_shi) {
          //   this.$Message.warning('请检查预警报警方式')
          //   this.loading = false
          //   return false
          // }
          // if (!arr[i].yj_tui_song) {
          //   this.$Message.warning('请检查预警报警推送')
          //   this.loading = false
          //   return false
          // }
        }
      }
      // 设定问题管控
      if (this.dbName === 'GovProblem') {
        for (let i = 0; i < arr.length; i++) {
          if (arr[i].wtgk_type) {
            arr[i].type = arr[i].wtgk_type.toString()
          } else {
            this.$Message.warning('请检查问题类型')
            this.loading = false
            return false
          }
          if (!arr[i].send_type) {
            this.$Message.warning('请检查发送对象')
            this.loading = false
            return false
          }
        }
      }
      // if (this.$route.query.what === 'edit') {
      let param = {
        listAll: arr,
        base_info_id: this.baseInfoId
      }
      update(this.dbName, param).then(res => {
        this.loading = false
        if (res.data.errcode === 0) {
          this.$Message.info('保存成功')
          this.$emit('result', res.data.data.res)
          this.$emit('refresh', true)
        } else {
          this.$Message.warning('保存失败!')
          this.$emit('refresh', false)
        }
      }).catch(ret => {
        this.loading = false
        // this.$Message.error(ret)
        this.$emit('refresh', false)
      })
      // } else {
      //   // JSON.stringify(saveData)
      //   let param = {
      //     listAll: saveData
      //   }
      //   create(this.dbName, param).then(res => {
      //     this.loading = false
      //     if (res.data.errcode === 0) {
      //       this.$Message.info('保存成功')
      //       this.$emit('result', res.data.data.res)
      //       this.$emit('refresh', true)
      //     } else {
      //       this.$Message.error('保存失败!')
      //       this.$emit('refresh', false)
      //     }
      //   }).catch(ret => {
      //     this.loading = false
      //     this.$Message.error(ret)
      //     this.$emit('refresh', false)
      //   })
      // }
    }
  },
  mounted () {
    let info = this.$route.query.what
    if (info === 'info') {
      this.isShow = false
    } else if (info === 'edit') {
      this.btnName = '更新'
    }
  }
}
</script>

<style scoped>

</style>
